<%@page import="com.szboanda.platform.common.utils.StringUtils"%>
<%@page import="com.szboanda.platform.v3.util.BeanUtils"%>
<%@page import="com.szboanda.platform.v3.util.DateUtils"%>
<%@ page language="java" import="com.szboanda.platform.v3.util.TWdstore"%>
<%@page language="java" import="com.szboanda.platform.v3.util.dao.JdbcDAO" pageEncoding="utf-8"%>
<%@page import="com.szboanda.platform.v3.util.helper.NamedQueryHelper"%>
<%@page import="com.szboanda.platform.v3.util.helper.QueryHelper"%>
<%@page import="com.szboanda.platform.v3.util.resources.SystemCode"%>
<%@page import="com.szboanda.platform.v3.util.resources.SystemCodesCache"%>
<%@page import="com.szboanda.platform.v3.workflow.definition.IDepartment"%>
<%@page import="com.szboanda.platform.v3.workflow.definition.IUser"%>
<%@page import="com.szboanda.platform.v3.workflow.jdbc.JDBCUsersCache"%>
<%@page import="com.zhuozhengsoft.pageoffice.wordwriter.DataRegion"%>
<%@page import="com.zhuozhengsoft.pageoffice.wordwriter.Font"%>

<%@ page import="org.apache.commons.beanutils.DynaBean"%>
<%@ page import="java.sql.Types"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%><%!
	TWdstore getWdstore(String appid, String modid,String zlx){
		TWdstore wd = null;
		if(StringUtils.isNotEmpty(appid)){
			String sql = "SELECT * FROM T_WDSTORE WHERE APPBH=? AND WDSX=? ORDER BY CJSJ DESC";
			if(StringUtils.isNotEmpty(zlx)){
				sql = "SELECT * FROM T_WDSTORE WHERE APPBH=? AND WDSX=? AND ZLX='"+zlx+"' ORDER BY CJSJ DESC";
			}
			QueryHelper qHelper = new QueryHelper(sql);
			qHelper.addParameter(Types.VARCHAR, appid);
			qHelper.addParameter(Types.VARCHAR, modid);
			DynaBean bean = new JdbcDAO().getSingleRow(null, qHelper);
			if(bean != null){
				wd = BeanUtils.convertDybeanToModel(bean, TWdstore.class);
			}
		}
		return wd;
	}

	//根据匹配类型格式化数据
	String regionValPplx(String regionVal, String zd, String pplx){
		String resultVal = "";
		if(StringUtils.isEmpty(regionVal)){
			return resultVal;
		}
		//默认值
		if("MRZ".equals(pplx)){
			resultVal = regionVal;
		}else if("BMBHQBMMC".equals(pplx)){//部门编号取部门名称
			IDepartment dept = JDBCUsersCache.getInstance().getDepartment(regionVal);
			if(dept != null){
				resultVal = dept.getDeptName();
			}
		}else if("YHBHQYHMC".equals(pplx)){//用户编号取用户名称
			IUser user = JDBCUsersCache.getInstance().getUser(regionVal);
			if(user != null){
				resultVal = user.getUserName();
			}
		}else if("RQZHDXRQ".equals(pplx)){//日期转换大写日期
			Date rq = DateUtils.parseDate(regionVal, "yyyy-MM-dd");
			resultVal = DateUtils.formatChineseDate(rq);
		}else if("RQZH".equals(pplx)){//日期转换（2015年1月1日）
			Date rq = DateUtils.parseDate(regionVal, "yyyy-MM-dd");
			resultVal = DateUtils.simpleDateFormat(rq, "yyyy年M月d日");
		}else if("RQZH1".equals(pplx)){//日期转换（2015年01月01日）
			Date rq = DateUtils.parseDate(regionVal, "yyyy-MM-dd");
			resultVal = DateUtils.simpleDateFormat(rq, "yyyy年MM月dd日");
		}else if("RQZH2".equals(pplx)){//日期转换（2015-01-01）
			Date rq = DateUtils.parseDate(regionVal, "yyyy-MM-dd");
			resultVal = DateUtils.simpleDateFormat(rq, "yyyy-MM-dd");
		}else if("GGDMPP".equals(pplx)){//从公共代码值表取代码内容（字段名和代码集名称一致才有效）
			Map<String,List<SystemCode>> map = SystemCodesCache.getInstance().getAllCahe();
			List<SystemCode> zdValList = map.get(zd);
			for(SystemCode code : zdValList){
				if(code.getCodeId().equals(regionVal)){
					resultVal = code.getValue();
				}
			}
		}
		return resultVal;
	}

	//查询模板书签与字段对应关系数据
	List<DynaBean> queryMbSqdyzd(String gwlxxh,String fl){
		NamedQueryHelper nqh = new NamedQueryHelper("SELECT * FROM T_OA_GWGL_MBSQDYZD WHERE XTXH=#XTXH# AND FL=#FL# ");
		nqh.bindParameter("XTXH", Types.VARCHAR, gwlxxh);
		nqh.bindParameter("FL", Types.VARCHAR, fl);
		return new JdbcDAO().getListValue(null, nqh);
	}

	//在线起草公文模板绑定书签专用
	WordDocument bindingDataRegion(String jsonData){
		WordDocument doc = new WordDocument();
		JSONObject jsObj = JSONObject.fromObject(jsonData);
		boolean isZjbd = true;
		String gwlxxh = (String)jsObj.get("PO_XTXH");
		if(StringUtils.isNotEmpty(gwlxxh)){
			//若是公文模板则去T_OA_GWGL_MBSQDYZD表中查找书签与字段对应关系
			List<DynaBean> fieldList = queryMbSqdyzd(gwlxxh,"GWMB");
			if(!BeanUtils.emptyCollection(fieldList)){//如果绑定关系数据存在
				for(DynaBean bean : fieldList){
					String zd = BeanUtils.getString(bean,"ZD");//字段
					String sq = BeanUtils.getString(bean,"SQ");//书签
					String pplx = BeanUtils.getString(bean,"PPLX");//匹配类型
					Calendar cal = Calendar.getInstance();
					String regionVal = "";
					if("YYYY".equals(zd)){
						regionVal = DateUtils.simpleDateFormat(cal.getTime(), "yyyy");
					}else if("MM".equals(zd)){
						regionVal = DateUtils.simpleDateFormat(cal.getTime(), "MM");
					}else if("DD".equals(zd)){
						regionVal = DateUtils.simpleDateFormat(cal.getTime(), "dd");
					}else{
						regionVal = regionValPplx((String)jsObj.get("PO_"+zd), zd, pplx);
					}
					if(StringUtils.isNotEmpty(regionVal)){
						doc.openDataRegion(sq).setValue(regionVal);
					}
				}
				isZjbd = false;//设为非直接绑定模式
			}
		}
		//非公文模板类型 或公文模板没有设置书签与字段对应关系的情况下-->直接将数据绑定对应书签
		if(isZjbd){
			Iterator<String> iter = jsObj.keys();
	    	while(iter.hasNext()){
	    		String key = iter.next();
	    		doc.openDataRegion(key).setValue((String)jsObj.get(key));
	    	}
		}
    	return doc;
	}

	//查询发文套红模板书签对应字段的值
	void queryFwThmbDataRegion(HttpServletRequest request, HttpServletResponse response){
		PrintWriter out = null;
		String result = "{\"result\":false}";
		JdbcDAO dao = new JdbcDAO();
		try{
			String xh = request.getParameter("xh");

			String sql = "SELECT * FROM T_OA_GWGL_FWDJJBXX WHERE XH=#XH#";
			NamedQueryHelper nqh = new NamedQueryHelper(sql);
			nqh.bindParameter("XH", Types.VARCHAR, xh);
			DynaBean fwBean = dao.getSingleRow(null, nqh);
			String gwlxxh = BeanUtils.getString(fwBean,"XTXH");

			Map<String, String> regionMap = new HashMap<String, String>();
			List<DynaBean> fieldList = queryMbSqdyzd(gwlxxh,"THBM");
			for(DynaBean bean : fieldList){
				String zd = BeanUtils.getString(bean,"ZD");//字段
				String sq = BeanUtils.getString(bean,"SQ");//书签
				String pplx = BeanUtils.getString(bean,"PPLX");//匹配类型
				Calendar cal = Calendar.getInstance();
				String regionVal = "";
				if("YYYY".equals(zd)){
					regionVal = DateUtils.simpleDateFormat(cal.getTime(), "yyyy");
				}else if("MM".equals(zd)){
					regionVal = DateUtils.simpleDateFormat(cal.getTime(), "MM");
				}else if("DD".equals(zd)){
					regionVal = DateUtils.simpleDateFormat(cal.getTime(), "dd");
				}else if("ZS".equals(zd)||"CS".equals(zd)||"CB".equals(zd)){
					regionVal = fwBean.get(zd).toString().replaceAll("\r\n","、");
				}else if(fwBean.get(zd) != null){
					regionVal = fwBean.get(zd).toString();
					regionVal = regionValPplx(regionVal, zd, pplx);
				}
				if(StringUtils.isNotEmpty(regionVal)){
					regionMap.put(sq, regionVal);
				}
			}

			String resultStr = JSONObject.fromObject(regionMap).toString();
			result = "{\"result\":"+resultStr+"}";
			out = response.getWriter();
			out.print(result);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
%>
